package com.mofang.powerdekorhelper.fragment;

import android.app.Dialog;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.mofang.powerdekorhelper.R;
import com.mofang.powerdekorhelper.activity.activity.ActivityListAct;
import com.mofang.powerdekorhelper.activity.customer.CustomerManagerActivity;
import com.mofang.powerdekorhelper.activity.mine.MinePointActivity;
import com.mofang.powerdekorhelper.activity.news.NewsInforActivity;
import com.mofang.powerdekorhelper.activity.order.OrderManagerActivity;
import com.mofang.powerdekorhelper.activity.train.TrainingSystemActivity;
import com.mofang.powerdekorhelper.adapter.NewCustomerListAdapter;
import com.mofang.powerdekorhelper.adapter.PresentUerManagerTaskAdapter;
import com.mofang.powerdekorhelper.adapter.UserManagerTaskAdapter;
import com.mofang.powerdekorhelper.base.MvpFragment;
import com.mofang.powerdekorhelper.base.recycleview.BaseRecycleViewList;
import com.mofang.powerdekorhelper.model.NewCustomer;
import com.mofang.powerdekorhelper.model.PresentManagerTask;
import com.mofang.powerdekorhelper.model.ResultMessage;
import com.mofang.powerdekorhelper.model.TodayData;
import com.mofang.powerdekorhelper.model.UserManagerTask;
import com.mofang.powerdekorhelper.persenter.WorkPlanPersenter;
import com.mofang.powerdekorhelper.utils.DialogUtils;
import com.mofang.powerdekorhelper.utils.ParamsUtils;
import com.mofang.powerdekorhelper.utils.SharePerforenceUtils;
import com.mofang.powerdekorhelper.utils.StringUtils;
import com.mofang.powerdekorhelper.utils.T;
import com.mofang.powerdekorhelper.view.WorkPlantView;
import com.mofang.powerdekorhelper.witget.MySwipeRefreshLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by luofei on 2017/8/16 0016.
 * 工作台
 */

public class WorkPlantFragment extends MvpFragment<WorkPlantView, WorkPlanPersenter> implements WorkPlantView, SwipeRefreshLayout.OnRefreshListener {

    @BindView(R.id.work_plant_refresh)
    MySwipeRefreshLayout refreshLayout;

    @BindView(R.id.new_customer_list)
    BaseRecycleViewList newCustomerList;

    @BindView(R.id.activity_list)
    BaseRecycleViewList activityList;

    @BindView(R.id.current_activity_list)
    BaseRecycleViewList currentActivityList;

    @BindView(R.id.customer_corder_tv)
    TextView newCorderTv;

    @BindView(R.id.invite_customers_tv)
    TextView newInviteTv;

    @BindView(R.id.share_num_tv)
    TextView newShareNumTv;

    @BindView(R.id.new_buy_card_tv)
    TextView newCardTv;

    private Integer userId;
    private Integer page = 1;
    private Integer pageSize = 10;
    private Integer state = null;
    private Dialog reportDialog;

    public static WorkPlantFragment fragment;

    public static WorkPlantFragment getInstance() {

        if (fragment == null) {
            fragment = new WorkPlantFragment();
        }
        return fragment;
    }

    @Override
    public View initView(LayoutInflater inflater) {

        View view = inflater.inflate(R.layout.fragment_work_plant_layout, null);
        ButterKnife.bind(this, view);

        refreshLayout.setOnRefreshListener(this);

        activityList.setLayoutManager(new LinearLayoutManager(mContext) {
            @Override
            public boolean canScrollVertically() {
                return false;
            }
        });

        currentActivityList.setLayoutManager(new LinearLayoutManager(mContext) {
            @Override
            public boolean canScrollVertically() {
                return false;
            }
        });

        return view;
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        userId = SharePerforenceUtils.getInstance(mContext).getUserID();
        state = 1;
        presenter.getUserManagerTask(ParamsUtils.getManagerTaskParams(userId, page, pageSize, state));
        state = 2;
        presenter.getPresentManagerTask(ParamsUtils.getManagerTaskParams(userId, page, pageSize, state));
        presenter.getTodayData(userId);

        List<NewCustomer> newCustomers = new ArrayList<>();
        NewCustomer newCustomer = new NewCustomer();
        newCustomer.setName("李先生");
        newCustomer.setPhone("15842457412");
        newCustomers.add(newCustomer);

        NewCustomer newCustomer2 = new NewCustomer();
        newCustomer2.setName("李先生");
        newCustomer2.setPhone("15842457412");
        newCustomers.add(newCustomer);
        NewCustomerListAdapter adapter = new NewCustomerListAdapter(newCustomers, mContext, R.layout.new_customer_list_item_layout);
        newCustomerList.setAdapter(adapter);
    }

    @Override
    public void setTodayData(TodayData todayData) {
        newCorderTv.setText(todayData.getResult().getOrderNum() + "");
        newInviteTv.setText(todayData.getResult().getCustomerNum() + "");
        newShareNumTv.setText(todayData.getResult().getShareNum() + "");
        newCardTv.setText(todayData.getResult().getBuyCardNum() + "");
    }

    @Override
    public void setCustomerReport(ResultMessage resultMessage) {
        hideProgress();
        toast("客户已报备");
    }

    @Override
    public void setUserManagerTask(UserManagerTask userManagerTask) {
        if (userManagerTask.getResult() != null) {
            UserManagerTaskAdapter adapter = new UserManagerTaskAdapter(userManagerTask.getResult().getData(), mContext, R.layout.work_activity_list_item_layout);
            activityList.setAdapter(adapter);
            hideProgress();
        }
    }

    @Override
    public void setPresentUerManagerTask(PresentManagerTask presentUerManagerTask) {
        if (presentUerManagerTask.getResult() != null) {
            if (presentUerManagerTask.getResult().getData().size() > 0) {
                PresentUerManagerTaskAdapter adapter = new PresentUerManagerTaskAdapter(presentUerManagerTask.getResult().getData(), mContext, R.layout.work_current_activity_list_item_layout);
                currentActivityList.setAdapter(adapter);
            }
            hideProgress();
        }
    }

    @Override
    public void onRefresh() {
        state = 1;
        presenter.getUserManagerTask(ParamsUtils.getManagerTaskParams(userId, page, pageSize, state));
        state = 2;
        presenter.getPresentManagerTask(ParamsUtils.getManagerTaskParams(userId, page, pageSize, state));
        presenter.getTodayData(userId);
    }

    @OnClick({R.id.knowledge_manneger_tv, R.id.customer_manager_tv, R.id.activity_manager_tv, R.id.integral_maneger_tv,
            R.id.new_order_continer, R.id.report_phone_btn, R.id.invite_customers_continer, R.id.trainin_system_btn})
    void onclick(View view) {
        switch (view.getId()) {

            case R.id.knowledge_manneger_tv:

                toActivity(NewsInforActivity.class);
                break;

            case R.id.customer_manager_tv:

                toActivity(CustomerManagerActivity.class);
                break;
            case R.id.activity_manager_tv:

                toActivity(ActivityListAct.class);
                break;
            case R.id.integral_maneger_tv:
                toActivity(MinePointActivity.class);
                break;

            case R.id.new_order_continer:
                toActivity(OrderManagerActivity.class);
                break;

            case R.id.invite_customers_continer:

                toActivity(CustomerManagerActivity.class);
                break;

            case R.id.report_phone_btn:

                reportDialog = DialogUtils.ReportDialog(mContext, reportInfo);
                reportDialog.show();
                break;

            case R.id.trainin_system_btn:

                toActivity(TrainingSystemActivity.class);
                break;
        }
    }

    DialogUtils.IReportInfo reportInfo = new DialogUtils.IReportInfo() {
        @Override
        public void getReportInfo(String customerName, String customerPhone) {

            if (!customerName.equals("") && !customerPhone.equals("")) {
                if (StringUtils.isPhoneNumAvaliable(customerPhone)) {
                    presenter.getCustomerReport(userId, customerName, customerPhone);
                } else {
                    toast("请输入正确手机号码");
                }
            } else {
                toast("请输入客户信息");
            }
        }

        @Override
        public void cancleDialog() {
            reportDialog.dismiss();
        }
    };

    @Override
    public void showProgress() {

    }

    @Override
    public void hideProgress() {
        if (reportDialog != null) {
            reportDialog.dismiss();
        }
        refreshLayout.hide();
    }

    @Override
    public void toast(CharSequence s) {

        T.showShort(mContext, s);
    }

    @Override
    public void toast(int id) {

    }

    @Override
    public void showNullLayout() {

    }

    @Override
    public void hideNullLayout() {

    }

    @Override
    public void showErrorLayout(View.OnClickListener listener) {

    }

    @Override
    public void hideErrorLayout() {

    }

    @Override
    public void onError(String errorInfo) {
        if (reportDialog != null) {
            reportDialog.dismiss();
        }
    }

    @Override
    public WorkPlanPersenter initPresenter() {
        return new WorkPlanPersenter();
    }

}
